package com.qf.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.Blogs;
import com.qf.entity.BlogsDTO;
import com.qf.listener.BlogsExcelListener;
import com.qf.service.BlogsService;
import com.qf.util.AliyunOSSUtil;
import com.qf.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * ------回首昨日,更进一步------
 *
 * @author JinXiaoChen
 * @date 2025/4/27
 * @desc
 */
@RestController
@CrossOrigin
public class BlogsController {
    @Autowired
    private BlogsService blogsService;
    /**
     * 查询所有用户信息
     *
     * @return
     */
    @PostMapping("/find/blogs/All")
    public R findAll() {
        List<Blogs> blogs = blogsService.findAll();
        return R.ok(blogs);
    }



    /**
     * 分页模糊查询用户信息
     */
    @GetMapping("/find/blogs/page")
    public R findPage(BlogsDTO blogsDTO) {
        PageHelper.startPage(blogsDTO.getPageNum(), blogsDTO.getPageSize());
        List<Blogs> blogs = blogsService.findPage(blogsDTO);
        PageInfo<Blogs> pageInfo = new PageInfo<>(blogs);
        return R.ok(pageInfo);
    }



    /**
     * 增加用户信息
     */

    @GetMapping("/blogs/add")
    public R addBlogs(Blogs blogs) {
        Date now = new Date(); // 最简单的方式
        blogs.setCreatedat(now);
        blogs.setYonghuname("测试用户");
        blogs.setYonghuid(9527);
        boolean flag = blogsService.addBlogs(blogs);

        return R.ok(flag);
    }
    /**
     * 删除用户信息
     */
    @GetMapping("/blogs/delete")
    public R deleteBlogs(Integer id) {
        boolean flag = blogsService.deleteBlogs(id);
        return R.ok(flag);
    }

    /**
     * 批量删除用户信息
     */
    @PostMapping("/blogs/delete/batch")
    public R deleteBlogsBatch(@RequestBody List<Integer> ids) {
        boolean flag = blogsService.deleteBlogsBatch(ids);
        return R.ok(flag);
    }

    /**
     * 修改用户信息
     */

    @PostMapping("/blogs/update")
    public R updateBlogs(Blogs blogs, @RequestParam(value = "file", required = false) MultipartFile file) {
        boolean flag = blogsService.updateBlogs(blogs);
        return R.ok(flag);
    }
    @GetMapping ("/blogs/find")
    public R findByTime(String time) {
        List<HashMap<String,Object>> list = blogsService.findByTime(time);
        return R.ok(list);
    }

}

